<template>
  <div class="my-header">
    <input type="text" class="input-add" v-model="title" placeholder="请输入您的任务名称，按回车键确认" @keyup.enter="add" />
  </div>
</template>

<script>
// 导入nanoid包，返回的是一个函数
import { nanoid } from 'nanoid'
export default {
  name: 'MyHeader',
  data () {
    return {
      title: ''
    }
  },
  methods: {
    add () {
      if (!this.title) return
      const todoObj = { id: nanoid(), title: this.title, done: false }
      this.title = ''
      this.$emit('addItem', todoObj)
    }
  }
}
</script>

<style scoped>
.input-add {
  width: 100%;
  padding: 10px 5px;
  border: 2px solid #d1d1d1;
  border-radius: 5px;
  outline: none;
}
</style>
